package com.hll.service.user;

import com.hll.dao.BaseDao;
import com.hll.dao.user.UserDao;
import com.hll.dao.user.UserDaoImpl;
import com.hll.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: Mr.Hu
 * @create: 2021-03-07 18:09
 */
public class UserServiceImpl implements UserService {
  private UserDao userDao; // 业务层调用dao层

  public UserServiceImpl() {
    // 实例化
    userDao = new UserDaoImpl();
  }

  @Override
  public User login(String userCode, String password) {
    User user = null;
    Connection connection = null;
    try {
      connection = BaseDao.getConnection();
      user = userDao.getLoginUser(connection, userCode, password);
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    } finally {
      // 怕dao层没有release
      BaseDao.release(connection, null, null);
    }
    return user;
  }

  @Override
  public boolean updatePwd(int id, String pwd) {
    boolean flag = false;
    Connection connection = null;
    try {
      connection = BaseDao.getConnection();
      flag = userDao.updatePwd(connection, id, pwd) > 0;
    } catch (SQLException throwables) {
      throwables.printStackTrace();
    } finally {
      BaseDao.release(connection, null, null);
    }
    return flag;
  }

  @Override
  public int getUserCount(String userName, Integer role) {
    Connection connection = null;
    int count = 0;
    try {
      connection = BaseDao.getConnection();
      count = userDao.getUserCount(connection, userName, role);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      BaseDao.release(connection, null, null);
    }
    return count; // 要被气死了,写成了return 0
  }

  @Override
  public List<User> getUserList(String userName, Integer role, int currentPageNo, int pageSize) {
    Connection connection = null;
    List<User> userList = null;
    try {
      connection = BaseDao.getConnection();
      userList = userDao.getUserList(connection, userName, role, currentPageNo, pageSize);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      BaseDao.release(connection, null, null);
    }
    return userList; // 要被气死了,写成了return 0
  }
}
